home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000…tember: Reference Library / Dev.CD Sep 00 RL Disk 1.toast / mac / What's New / • What was new 08⁄00 / Sample Code / Interapplication Comm / MoreOSL / MoreProcesses / MoreProcesses.cp next >
Encoding:
Text File  |  2000-06-23  |  9.4 KB  |  339 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        MoreProcesses.cp
  3.  
  4.     Contains:    
  5.  
  6.     Written by:    Pete Gontier
  7.  
  8.     Copyright:    Copyright (c) 1998 Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.  
  20.          <6>     20/3/00    Quinn   Added MoreProcIsProcessAtFront.  Tidied up some copy and paste
  21.                                     errors in the comments.  Support passing nil to more routines to
  22.                                     mean "current process".
  23.          <5>      3/9/00    gaw     API changes for MoreAppleEvents
  24.          <4>      6/3/00    Quinn   Added MoreProcGetProcessAppFile.
  25.          <3>     1/22/99    PCG     TARGET_CARBON
  26.          <2>    11/11/98    PCG     fix header
  27.          <1>    11/10/98    PCG     first big re-org at behest of Quinn
  28.  
  29.     Old Change History (most recent first):
  30.  
  31.          <3>     11/9/98    PCG     fix comment 2
  32.          <2>     11/9/98    PCG     add copyright blurb
  33.          <1>     6/16/98    PCG     initial checkin
  34. */
  35.  
  36. #include "MoreProcesses.h"
  37. /******************************************************************************
  38.     Return a ProcessSerialNumber for a process whose signature (creator)
  39.     matches the input values.  This routine will find the first process that
  40.     matches the creator.
  41.     
  42.     The ProcessSerialNumber will be kNoProcess is the requested process cannot
  43.     be found. 
  44.  
  45.     pCreator            input:    The creator of the process to be found.
  46.     pPSN                input:    Pointer to a ProcessSerialNumber.
  47.                         output:    A valid PSN or kNoProcess in no match is found.
  48.  
  49.     RESULT CODES
  50.     ____________
  51.     noErr               0    No error
  52.     procNotFound    –600    No process matched specified creator
  53.     ____________
  54. */
  55.  
  56. pascal OSStatus MoreProcFindProcessByCreator(const OSType pCreator,ProcessSerialNumber *pPSN)
  57. {
  58.     OSStatus err = noErr;
  59.  
  60.     if (!(MoreAssert (pPSN)))
  61.         err = paramErr;
  62.     else
  63.     {
  64.         pPSN->lowLongOfPSN    = kNoProcess;
  65.         pPSN->highLongOfPSN    = kNoProcess;
  66.  
  67.         while (!(err = GetNextProcess (pPSN)))
  68.         {
  69.             ProcessInfoRec pir;
  70.  
  71.             if (!(err = MoreProcGetProcessInformation (pPSN,&pir)))
  72.                 if (pCreator == pir.processSignature)
  73.                     break;
  74.         }
  75.     }
  76.  
  77.     return err;
  78. }
  79. /******************************************************************************
  80.     Return a ProcessSerialNumber for a process whose signature (type and creator)
  81.     matches the input values.  This routine will find the first process that
  82.     matches the type and creator.
  83.     
  84.     The ProcessSerialNumber will be kNoProcess is the requested process cannot
  85.     be found. 
  86.  
  87.     pCreator                input:    The creator type of the process to be found.
  88.     pType                input:    The file type of the process to be found.
  89.     pPSN                input:    Pointer to a ProcessSerialNumber.
  90.                         output:    A valid PSN or kNoProcess in no match is found.
  91.  
  92.     RESULT CODES
  93.     ____________
  94.     noErr               0    No error
  95.     procNotFound    –600    No process matched specified type and creator
  96.     ____________
  97. */
  98.  
  99. pascal OSStatus MoreProcFindProcessBySignature(const OSType pCreator,const OSType pType,ProcessSerialNumber *pPSN)
  100. {
  101.     OSStatus err = noErr;
  102.  
  103.     if (!(MoreAssert (pPSN)))
  104.         err = paramErr;
  105.     else
  106.     {
  107.         pPSN->lowLongOfPSN    = kNoProcess;
  108.         pPSN->highLongOfPSN    = kNoProcess;
  109.  
  110.         while (!(err = GetNextProcess (pPSN)))
  111.         {
  112.             ProcessInfoRec pir;
  113.  
  114.             if (!(err = MoreProcGetProcessInformation (pPSN,&pir)))
  115.                 if ((pCreator == pir.processSignature) && (pType == pir.processType))
  116.                     break;
  117.         }
  118.     }
  119.  
  120.     return err;
  121. }
  122. /******************************************************************************
  123.     Returns the name of the process specified by ProcessSerialNumberPtr.
  124.     
  125.     The string pointed to by pProcessName will be untouched if the process
  126.     can't be found. 
  127.  
  128.     pPSN            input:    The process whose name you want (nil for current).
  129.     pProcessName    input:    Pointer to a Str31 for the process name.
  130.                     output:    The process name.
  131.     
  132.     RESULT CODES
  133.     ____________
  134.     noErr               0    No error
  135.     paramErr         –50    Process serial number is invalid
  136.     ____________
  137. */
  138. pascal    OSStatus    MoreProcGetProcessName(
  139.                                 const ProcessSerialNumberPtr pPSN,
  140.                                 StringPtr pProcessName)
  141. {
  142.     OSStatus        anErr = noErr;    
  143.     ProcessInfoRec    infoRec;
  144.     ProcessSerialNumber localPSN;
  145.  
  146.     MoreAssertQ(pProcessName != nil);
  147.         
  148.     infoRec.processInfoLength = sizeof(ProcessInfoRec);
  149.     infoRec.processName = pProcessName;
  150.     infoRec.processAppSpec = nil;
  151.     
  152.     if ( pPSN == nil ) {
  153.         localPSN.highLongOfPSN = 0;
  154.         localPSN.lowLongOfPSN  = kCurrentProcess;
  155.     } else {
  156.         localPSN = *pPSN;
  157.     }
  158.     
  159.     anErr = GetProcessInformation(&localPSN, &infoRec);
  160.     
  161.     return anErr;
  162. }//end MoreProcGetProcessName
  163. /******************************************************************************
  164.     Returns information about the process specified by ProcessSerialNumberPtr.
  165.     
  166.     pProcessType and pCreator will be untouched if the process
  167.     can't be found. 
  168.  
  169.     pPSN                input:    The process whose info you want (nil for current).
  170.     pProcessType        output:    The process's type.
  171.     pCreator            output:    The process's signature.
  172.     
  173.     RESULT CODES
  174.     ____________
  175.     noErr               0    No error
  176.     paramErr         –50    Process serial number is invalid
  177.     ____________
  178. */
  179. pascal    OSStatus    MoreProcGetProcessTypeSignature(
  180.                         const ProcessSerialNumberPtr pPSN,
  181.                         OSType *pProcessType,
  182.                         OSType *pCreator)
  183. {
  184.     OSStatus            anErr = noErr;    
  185.     ProcessInfoRec        infoRec;
  186.     ProcessSerialNumber localPSN;
  187.     
  188.     infoRec.processInfoLength = sizeof(ProcessInfoRec);
  189.     infoRec.processName = nil;
  190.     infoRec.processAppSpec = nil;
  191.  
  192.     if ( pPSN == nil ) {
  193.         localPSN.highLongOfPSN = 0;
  194.         localPSN.lowLongOfPSN  = kCurrentProcess;
  195.     } else {
  196.         localPSN = *pPSN;
  197.     }
  198.     
  199.     anErr = GetProcessInformation(&localPSN, &infoRec);
  200.     if (anErr == noErr)
  201.     {
  202.         *pProcessType = infoRec.processType;
  203.         *pCreator = infoRec.processSignature;
  204.     }
  205.     
  206.     return anErr;
  207. }//end MoreProcGetProcessTypeSignature
  208.  
  209. /******************************************************************************
  210.     Returns the FSSpec for the current process
  211.     
  212.     pFSSpec                output:    The process's FSSpec.
  213.     
  214.     RESULT CODES
  215.     ____________
  216.     noErr               0    No error
  217.     paramErr         –50    pFSSpec is nil
  218.     ____________
  219. */
  220. pascal OSStatus MoreProcGetCurrentProcessFSSpec(FSSpec *pFSSpec)
  221. {
  222.     return MoreProcGetProcessAppFile(nil,pFSSpec);
  223. }
  224. /******************************************************************************
  225.     Returns the FSSpec for the process specified by ProcessSerialNumberPtr.
  226.     
  227.     pPSN                input:    The process's Serial Number
  228.                                 if nil return the AppFile for the current process
  229.     pAppFile            output:    The process's appFile.
  230.     
  231.     RESULT CODES
  232.     ____________
  233.     noErr               0    No error
  234.     paramErr         –50    Process serial number is invalid (or pPSN or pAppFile is nil)
  235.     ____________
  236. */
  237. pascal OSStatus MoreProcGetProcessAppFile(const ProcessSerialNumber *pPSN,FSSpec *pAppFile)
  238. {
  239.     OSStatus             err;
  240.     ProcessInfoRec         processInfo;
  241.     ProcessSerialNumber localPSN;
  242.     
  243.     if (nil == pAppFile)
  244.         return paramErr;
  245.     
  246.     if ( pPSN == nil ) {
  247.         localPSN.highLongOfPSN = 0;
  248.         localPSN.lowLongOfPSN  = kCurrentProcess;
  249.     } else {
  250.         localPSN = *pPSN;
  251.     }
  252.     processInfo.processInfoLength    = sizeof(processInfo);
  253.     processInfo.processName            = nil;
  254.     processInfo.processAppSpec        = pAppFile;
  255.  
  256.     err = GetProcessInformation(&localPSN, &processInfo);
  257.  
  258.     return err;
  259. }
  260. /******************************************************************************
  261.     Returns the Process Information for the process specified by pPSN.
  262.     
  263.     pPSN                input:    The process's Serial Number
  264.     pPIR                output:    The process's Information.
  265.     
  266.     RESULT CODES
  267.     ____________
  268.     noErr               0    No error
  269.     paramErr         –50    Process serial number is invalid (or pPSN or pPIR is nil)
  270.     ____________
  271. */
  272. pascal OSStatus MoreProcGetProcessInformation(const ProcessSerialNumber *pPSN,ProcessInfoRec *pPIR)
  273. {
  274.     if (!MoreAssert (pPIR))
  275.         return paramErr;
  276.  
  277.     pPIR->processInfoLength    = sizeof (*pPIR);
  278.     pPIR->processName        = nil;
  279.     pPIR->processAppSpec    = nil;
  280.  
  281.     if (pPSN)
  282.         return GetProcessInformation (pPSN,pPIR);
  283.     else
  284.     {
  285.         ProcessSerialNumber psn = { kNoProcess,kCurrentProcess };
  286.         return GetProcessInformation (&psn,pPIR);
  287.     }
  288. }
  289. /******************************************************************************
  290.     Returns true if the process specified by pPSN is a background only application.
  291.     
  292.     pPSN                input:    The process's Serial Number
  293.     pIsBOA                output:    The process's Information.
  294.     
  295.     RESULT CODES
  296.     ____________
  297.     noErr               0    No error
  298.     paramErr         –50    Process serial number is invalid (or pPSN or pIsBOA is nil)
  299.     ____________
  300. */
  301. pascal OSStatus MoreProcIsProcessBackgroundOnly(const ProcessSerialNumber *pPSN,Boolean *pIsBOA)
  302. {
  303.     OSStatus err = noErr;
  304.     ProcessInfoRec pir;
  305.  
  306.     if (!MoreAssert (pIsBOA))
  307.         return paramErr;
  308.  
  309.     if (!(err = MoreProcGetProcessInformation (pPSN,&pir)))
  310.     {
  311.         *pIsBOA = (modeOnlyBackground & pir.processMode) ? true : false;
  312.     }
  313.  
  314.     return err;
  315. }
  316.  
  317. extern pascal Boolean MoreProcIsProcessAtFront(const ProcessSerialNumber *pPSN)
  318.     // See comment in header.
  319. {    
  320.     OSStatus             err;
  321.     ProcessSerialNumber localPSN;
  322.     ProcessSerialNumber frontPSN;
  323.     Boolean              isSame;
  324.     
  325.     if ( pPSN == nil ) {
  326.         localPSN.highLongOfPSN = 0;
  327.         localPSN.lowLongOfPSN  = kCurrentProcess;
  328.     } else {
  329.         localPSN = *pPSN;
  330.     }
  331.     
  332.     err = GetFrontProcess(&frontPSN);
  333.     if (err == noErr) {
  334.         err = SameProcess(&localPSN, &frontPSN, &isSame);
  335.     }
  336.     MoreAssertQ(err == noErr);            // If either of the previous return an error, we want to know why.
  337.     return (err == noErr) && isSame;
  338. }
  339.